﻿@page "/"

@using Masa.Blazor.Extensions.Languages.Razor

<MRow NoGutters>
    <MCol>
        <MCard Class="pa-1"
               Outlined
               Style="height: 100vh"
               tile>

            <ComponentLibrary OnClick="CreateOnClick"></ComponentLibrary>

        </MCard>
    </MCol>
    <MCol Order="2"
          Cols="12"
          Sm="6"
          Md="8">
        <MCard Class="pa-2"
               Outlined
               Style="height: 100vh"
               tile>
            @foreach (var item in Renders.Where(x => x.Value.Visible))
            {
                <render @onclick="() => Id = item.Key">
                    @item.Value.RenderFragment
                </render>
            }
        </MCard>
    </MCol>
    <MCol Order="3">
        <MCard Class="pa-2"
               Outlined
               Style="height:100vh"
               tile>
            <MCard>
                @foreach (var item in Renders)
                {
                    if (item.Key == Id)
                    {
                        <MTextField @bind-Value="item.Value.GenerateOptions.Width" Label="width"></MTextField>
                        <MTextField @bind-Value="item.Value.GenerateOptions.Height" Label="height"></MTextField>
                        <MTextField @bind-Value="item.Value.GenerateOptions.Style" Label="style"></MTextField>
                        <MTextField @bind-Value="item.Value.GenerateOptions.Class" Label="class"></MTextField>
                        <MDivider></MDivider>
                        <MButton OnClick="() => AddOptionsAttribute(item.Value.GenerateOptions.attributes)" Block>新增扩展参数输入框</MButton>
                        <MDivider></MDivider>
                        <MButton Block OnClick="() => DeleteComponent(item.Key)">删除</MButton>
                    }
                }
            </MCard>
        </MCard>
    </MCol>
</MRow>

@code {
    private string Id { get; set; }

    private Dictionary<string, Render> Renders = new();

    private void AddOptionsAttribute(Dictionary<string, object> attribute)
    {
        attribute.Add("new", "");
    }

    private void DeleteComponent(string key)
    {
        Renders.Remove(key);
    }

    private void CreateOnClick(TemplatesLanguageModule module)
    {
        if (!string.IsNullOrEmpty(Id) && Renders.TryGetValue(Id, out var valur))
        {
            var ComponentType = RazorCompile.CompileToType(new CompileRazorOptions()
            {
                Code = module.Template,
                ConcurrentBuild = true
            });
            var render = new Render();

            render.GenerateOptions.attributes.Add("Width", render.GenerateOptions.Width);
            render.GenerateOptions.attributes.Add("ChildContent", render.RenderFragment);
            
            render.RenderFragment = @<DynamicComponent Type="ComponentType" Parameters="render.GenerateOptions.attributes"></DynamicComponent>;
            render.Visible = false;

            valur.RenderFragment = render.RenderFragment;
            Renders.Add(Guid.NewGuid().ToString("N"), render);
        }
        else
        {
            var ComponentType = RazorCompile.CompileToType(new CompileRazorOptions()
            {
                Code = module.Template,
                ConcurrentBuild = true
            });
            var render = new Render();

            render.GenerateOptions.attributes.Add("Width", render.GenerateOptions.Width);

            render.GenerateOptions.attributes.Add("ChildContent", render.RenderFragment);
            render.RenderFragment = @<DynamicComponent Type="ComponentType" Parameters="render.GenerateOptions.attributes"></DynamicComponent>;
            render.Visible = true;

            Renders.Add(Guid.NewGuid().ToString("N"), render);
        }
        StateHasChanged();
    }

}